<?php
/**
 * 管理中心· 编码管理模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com；
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Controller;
use Think\AjaxPage;
class CodeController extends BaseController {
    //初始化程序
    public function _initialize()
    {
        parent::_initialize();
        $grident = M('grade');
        $grList = $grident->getField('gr_id,gr_name');
        //dump($grList);
        $this->assign('grade',$grList); //编码类型
        
        $timegap['beg'] = date('Y-m-d H:i',strtotime("-4 month"));//4个月
        $timegap['end'] = date('Y-m-d H:i',strtotime("+1 days"));
        $this->assign('timegap',$timegap);
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取编码列表的功能
     * @author : XC跃
     * @date：2018-03-15
     +--------------------------------------------------------------------------
     */
		public function regList(){
			$type = trim(I('get.type','2'));
			$this->assign('type',$type);
			$this->display();
		}
		public function ajaxList(){
			//实例化相关表
			$ident = M('code');
			// 搜索条件
			$beg = strtotime(I('stime'));
			$end = strtotime(I('etime'));
			$condition = array();
			I('keyword') ? $condition[I('xcinput')] = array('LIKE',"%".I('keyword')."%") : false;
			I('state') ? $condition['co_state']= I('state') : false;
			if($beg && $end){
				$condition['co_atime'] = array('between',"$beg,$end");
			}
			$condition['co_gid'] = $type = I('post.type');
			$count = $ident->where($condition)->count();
			$Page  = new AjaxPage($count,50);
			$show = $Page->show();
			$list = $ident->where($condition)->order('co_jtime asc')->limit($Page->firstRow.','.$Page->listRows)->select();
			//echo $ident->getLastSql();
			$this->assign('list',$list);
			$this->assign('page',$show);// 赋值分页输出
			$this->assign('rows',$Page->totalRows);
			$this->assign('type',$type);
			$this->display();
		}
    /**
     +--------------------------------------------------------------------------
     * 实现添加编码的功能
     * @author : XC跃
     * @date：2018-03-15
     +--------------------------------------------------------------------------
     */
    public function codeAdd(){
        //实例化相关表
        $coident = M('code');
				
        if(IS_POST){
            $type = I('type/d','2');
            $cnum = I('cnum/d','100');
            if(empty($cnum) || $cnum <= 0){
                $res = array('status'=>-1,'msg'=>'请输入生成个数');
                exit(json_encode($res));
            }if ($cnum > 1000){
                $res = array('status'=>-1,'msg'=>'每次最多可生成1000个');
                exit(json_encode($res));
            }
            //执行生成
            $data = array();
            for($i=0;$i<$cnum;$i++) {
                $code_num = getActivate($lenth='9');
                $data[] = array('co_gid'=>$type,'co_code'=>$code_num,'co_atime'=>time(),'co_state'=>'2');
            }
            $re = $coident->addAll($data,array(),true);
            if($re>0){
                $res = array('status'=>1,'msg'=>'成功生成'.$cnum.'套编码');
                exit(json_encode($res));
            }
        }else{
            $this->display();
        }
    }
	
	/**
	 +--------------------------------------------------------------------------
	 * 实现导出编码的功能
	 * @author : XC跃
	 * @date：2018-05-06
	 +--------------------------------------------------------------------------
	 */
	public function codeExport(){
	    //配置参数
	    $coType = C('CODE_TYPE');
	    $coName = $coType[I('type')][0];
	    //dump($coType);die;
	    $xlsName  = $coName."列表";
	    $xlsCell  = array(
	        array('co_id','编号'),
	        array('co_code','编码信息'),
	        array('co_buid','购买人ID'),
	        array('co_uname','购买人姓名'),
	        array('co_juid','激活会员ID'),
	        array('co_jname','激活人姓名'),
	        array('co_atime','生成时间'),
	        array('co_jtime','激活时间'),
	        array('co_state','状态'),
	    );
	    //实例化相关表
	    $ident = M('code');
	    // 搜索条件
	    $beg = strtotime(I('stime'));
	    $end = strtotime(I('etime'));
	    $condition = array();
	    I('keyword') ? $condition[I('xcinput')] = array('LIKE',"%".I('keyword')."%") : false;
	    I('state') ? $condition['co_state']= I('state') : false;
	    if($beg && $end){
	        $condition['co_atime'] = array('between',"$beg,$end");
	    }
	    $condition['co_type'] = I('type');
	    //dump($condition);
	    $sort_order = I('order_by').' '.I('sort');
	
	    $xlsData = $ident->where($condition)->order($sort_order)->select();
	    //echo $ident->getLastSql();die;
	    foreach($xlsData as $k=>$v){
	        $xlsData[$k]['co_id']   = $k+1;
	        $xlsData[$k]['co_atime']= Date("Y-m-d H:i",$v['co_atime']);
	        if($v['co_jtime']!='0'){
	            $xlsData[$k]['co_jtime']= Date("Y-m-d H:i",$v['co_jtime']);
	        }
	        $xlsData[$k]['co_state']= $v['co_state']=='2'?'未使用':'已使用';
	    }
	    exportExcel($xlsName,$xlsCell,$xlsData);
	    //日志
	    addLog(session('admin_auth.usname'),'2','2',"导出".$coName.($k+1)."条数据");
	}
}